const api = require("../../api/api.js");
const util = require("../../utils/util.js");

Page({
  data: {
    type: ''
  },
  formSubmit(e) {
    let that = this;
    let account = e.detail.value.account.trim();
    let psd = e.detail.value.psd.trim();
    if (account === '' || psd === '') {
      util.showToast("请将信息填写完整", "none");
      return;
    }
    that.login({
      account: account,
      psd: psd
    });
  },
  loginByWeChat(e) {
    let that = this;
    let userInfo = e.detail.userInfo;
    console.log(userInfo);
    wx.showLoading({
      title: '登录中...',
    })
    wx.login({
      success(res) {
        let code = res.code;
        api.wxLogin({
          data: {
            js_code: code
          },
          success(res) {
            console.log(res.data);
            let isUser = res.data;
            if (isUser.type === 0) { //0表示微信登录用户已经完善账户信息
              wx.hideLoading();
              if (userInfo !== undefined) {
                let localStorages = [
                  ['type', 'wx'],
                  ['userId', isUser.account.account],
                  ['openid', isUser.openid],
                  ['role', isUser.role],
                  ['wxName', userInfo.nickName],
                  ['wxImg', userInfo.avatarUrl],
                  ['isBind', true]
                ];
                util.setLocalStorage(localStorages);
                that.goBack();
              } else {
                util.showToast('没有获取用户信息的权限！', 'none');
              }
            } else if (isUser.type === 2) { //2表示微信登录用户还未绑定账号信息
              if (userInfo !== undefined) {
                let localStorages = [
                  ['type', 'wx'],
                  ['openid', isUser.openid],
                  ['wxName', userInfo.nickName],
                  ['wxImg', userInfo.avatarUrl],
                  ['isBind', false]
                ];
                util.setLocalStorage(localStorages);
                that.goBack();
              } else {
                util.showToast('没有获取用户信息的权限！', 'none');
              }
            } else {
              that.loginByWeChat(e);
            }
          }
        });
      }
    });
  },
  login(params) {
    let that = this;
    api.login({
      data: {
        account: params.account,
        password: params.psd
      },
      success(res) {
        console.log(res.data);
        let data = res.data;
        if (data.type === 1) {
          util.showToast("账号不存在", "none");
        } else if (data.type === 2) {
          util.showToast("密码错误", "none");
        } else {
          let localStorages = [
            ['type', 'normal'],
            ['role', data.role],
            ['userId', params.account]
          ];
          util.setLocalStorage(localStorages);
          that.goBack();
        }
      }
    });
  },
  goBack() {
    let pages = getCurrentPages();
    pages[pages.length - 2].onLoad();
    wx.navigateBack({
      delta: -1
    })
  },
  full(e) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      type
    });
  },
  close() {
    this.setData({
      type: ''
    });
  }
})